package com.yinp.proapp.module.task.fragment;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.core.content.ContextCompat;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.yinp.proapp.R;
import com.yinp.proapp.adapter.ComViewHolder;
import com.yinp.proapp.adapter.CommonAdapter;
import com.yinp.proapp.adapter.SingleViewHolder;
import com.yinp.proapp.base.fragment.AppBaseFragment;
import com.yinp.proapp.databinding.FragmentToDoHistoryBinding;
import com.yinp.proapp.databinding.ItemHistoryUndeterminedListBinding;
import com.yinp.proapp.greendao.DealLocaleTask;
import com.yinp.proapp.module.task.bean.LocaleTaskBean;

import java.util.ArrayList;
import java.util.List;

/**
 * @author :yinpeng
 * date      :2021/9/2
 * package   :com.yinp.proapp.module.task.fragment
 * describe  :历史待做
 */
public class ToDoHistoryFragment extends AppBaseFragment<FragmentToDoHistoryBinding> {
    private CommonAdapter<LocaleTaskBean> adapter;
    private List<LocaleTaskBean> dataList = new ArrayList<>();
    private DealLocaleTask<LocaleTaskBean> dealLocaleTask;

    public static ToDoHistoryFragment getInstance() {
        ToDoHistoryFragment toDoHistoryFragment = new ToDoHistoryFragment();
        Bundle bundle = new Bundle();
        toDoHistoryFragment.setArguments(bundle);
        return toDoHistoryFragment;
    }

    @Override
    protected void initViews(View view) {
        bd.noTips.setText("暂无待历史做任务");
        dealLocaleTask = new DealLocaleTask<>();
        initRecycler();
        getDataList();
    }

    private void initRecycler() {
        adapter = new CommonAdapter<LocaleTaskBean>(getContext(),dataList) {
            @Override
            protected SingleViewHolder setComViewHolder(View view, int viewType, ViewGroup parent) {
                ItemHistoryUndeterminedListBinding binding = ItemHistoryUndeterminedListBinding.inflate(mInflater, parent, false);
                return new SingleViewHolder(binding);
            }

            @Override
            public void onBindItem(SingleViewHolder holder, int position, LocaleTaskBean item) {
                ItemHistoryUndeterminedListBinding binding = (ItemHistoryUndeterminedListBinding) holder.getBinding();
                if (!TextUtils.isEmpty(item.getTitle())) {
                    binding.tvTitle.setText(item.getTitle());
                    binding.tvTitle.setVisibility(View.VISIBLE);
                }
                binding.tvContent.setText(item.getContent());
                binding.tvDate.setText(item.getDate() + " " + item.getTime());
                if (item.getIsFinish()) {
                    binding.tvStatus.setText("已完成");
                    binding.ccvPoint.changeColor(getContext(), R.color._59d281);
                    binding.tvStatus.setTextColor(ContextCompat.getColor(getContext(), R.color._59d281));
                    binding.sllStatus.setBackgroundColor(ContextCompat.getColor(getContext(), R.color._1559d281));
                } else {
                    binding.tvStatus.setText("未完成");
                    binding.ccvPoint.changeColor(getContext(), R.color.fb5c5c);
                    binding.tvStatus.setTextColor(ContextCompat.getColor(getContext(), R.color.fb5c5c));
                    binding.sllStatus.setBackgroundColor(ContextCompat.getColor(getContext(), R.color._15fb5c5c));
                }
            }
        };
        bd.rvList.setLayoutManager(new LinearLayoutManager(getContext()));
        //添加了这个，如果从最后开始删除就没有问题，如果随意删除就会崩溃
//        bd.rvList.setHasFixedSize(true);
        bd.rvList.setAdapter(adapter);
        bd.rvList.addOnScrollListener(new RecyclerView.OnScrollListener() {
            @Override
            public void onScrolled(@NonNull RecyclerView recyclerView, int dx, int dy) {
                super.onScrolled(recyclerView, dx, dy);
                if (linearLayoutManager == null) {
                    linearLayoutManager = (LinearLayoutManager) recyclerView.getLayoutManager();
                }
                if (linearLayoutManager == null) {
                    return;
                }
                totalCount = linearLayoutManager.getItemCount();
                visiblePosition = linearLayoutManager.findLastVisibleItemPosition();
                if (visiblePosition != -1 && visiblePosition != 0) {
                    if (is && dy > 0 && totalCount % visiblePosition == 2) {
                        is = false;
                        page++;
                        getDataList();
                    } else if (totalCount % visiblePosition != 2) {
                        is = true;
                    }
                }
            }
        });
    }

    private int totalCount = -1;
    private int visiblePosition = -1;
    private LinearLayoutManager linearLayoutManager;
    private boolean is = false;
    private int page = 1;

    public void initDataList() {
        page = 1;
        getDataList();
    }

    /**
     * 获取今日数据列表
     */
    private void getDataList() {
        List<LocaleTaskBean> list = dealLocaleTask.queryHistoryList(page);
        if (list != null && list.size() > 0) {
            if (page == 1) {
                dataList.clear();
            }
            dataList.addAll(list);
            adapter.notifyItemRangeInserted(dataList.size(), list.size());
            bd.noLl.setVisibility(View.GONE);
            bd.rvList.setVisibility(View.VISIBLE);
        } else {
            if (dataList.size() == 0) {
                bd.noLl.setVisibility(View.VISIBLE);
                bd.rvList.setVisibility(View.GONE);
            }
        }
    }

    static class LocaleTaskViewHolder extends ComViewHolder {
        ItemHistoryUndeterminedListBinding binding;

        public LocaleTaskViewHolder(ItemHistoryUndeterminedListBinding itemView) {
            super(itemView.getRoot());
            binding = itemView;
        }
    }
}
